<?php

class view
{
    public $config;
    public  $vars;
    public  $foreachVars;
    private $tpl;
    private $content;
    private $fileName;
    private $compile;
    private $cache;
    private $compileDir;
    private $cacheDir;
    private $useCache   =   TRUE;

    public function __construct()
    {
        $this->vars = (object) NULL;
        $this->config = core::load("config");
    }

    function set($name, $var) {
        if (!isset($this->vars->{$name})) {
            $this->vars->{$name} = $var;
        } else {
            die('SetVar must be use with not declared tpl vars !');
        }
    }

    public function getVars(&$controller = null)
    {
        if (!empty($controller))
        {
            $this->vars->app = $controller;
        }
        return $this->vars;
    }

    public function setTemplate($template)
    {
        $this->fileName = $template;
    }

    public function getTemplate($controller = null)
    {
        if (empty($this->fileName))
        {
            return $controller;
        }
        return $this->fileName;
    }

    private function __get($var)
    {
        return core::load($var);
    }

    public  function loadTemplate($controller, $template, $vars, $layout=false, $admin=false)
    {

        if ($admin == true)
        {
            if (file_exists(TPL_DIR.'default'.DIR_SEP.'html_templates'.DIR_SEP.$controller.DIR_SEP.'admin'.DIR_SEP.$template.'.html'))
            {
                $templateFile = TPL_DIR.'default'.DIR_SEP.'html_templates'.DIR_SEP.$controller.DIR_SEP.'admin'.DIR_SEP.$template.'.html';
            }
            else
            {
                $templateFile = DIR.'modules'.DIR_SEP.$controller.DIR_SEP.'views'.DIR_SEP.'admin'.DIR_SEP.$template.'.html';
            }
        }
        else
        {
            if (file_exists(TPL_DIR.'default'.DIR_SEP.'html_templates'.DIR_SEP.$controller.DIR_SEP.$template.'.html'))
            {
                $templateFile = TPL_DIR.'default'.DIR_SEP.'html_templates'.DIR_SEP.$controller.DIR_SEP.$template.'.html';
            }
            else
            {
                $templateFile = DIR.'modules'.DIR_SEP.$controller.DIR_SEP.'views'.DIR_SEP.$template.'.html';
            }
        }

        if (isset($layout))
        {
            if (file_exists($templateFile))
            {
                $layoutData =  file_get_contents($templateFile);
            }
            else
            {
                Error::getInstance(E_ERROR ,'Template not found ('.$templateFile.')', __FILE__, __LINE__);

            }
            $layoutFile = TPL_DIR.'default'.DIR_SEP.'layouts'.DIR_SEP.$layout.'.html';

            if (file_exists($layoutFile))
            {
                $content = file_get_contents($layoutFile);
                $content = str_replace('{content}', $layoutData, $content);
            }
            else
            {
                $content = $layouData;
            }

            $cache = CACHE_DIR.'compiled'.DIR_SEP.str_replace(array('\\','/',':'), array('/','-',''), $template).".php";
            $tab = array (
                '0' => $content,
                '1' => $this
            );
            if (file_exists($cache))
            {
                if(@filemtime($templateFile) > @filemtime($cache) || @filemtime($layoutFile) > @filemtime($cache)){
                    $compiler = core::load('compiler', $tab);
                    file_put_contents($cache, $compiler->fetch());
                }
                include $cache;
            }
            else
            {
                $compiler = core::load('compiler', $tab);
                file_put_contents($cache, $compiler->fetch());
                include $cache;
            }
        }

        else
        {
            Error::getInstance(E_ERROR ,'Template not found ('.$templateFile.')', __FILE__, __LINE__);
        }
    }

}

?>
